#region FileHeader
//////////////////////////////////////////////////////////////////////////////////
// Author:                          Hector Llanos
// Date:                            08.27.2010
// FileName:                        CGamePlayState.cs
// Reason:                          main game play scene
//////////////////////////////////////////////////////////////////////////////////
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

using Run_Game;
using Manager;
using Base;
using Objects;
using Globals;
using MessagingSystem;

namespace GameState
{
    public class CGamePlayState : IGameState
    {
        private ContentManager m_pContent;
        private SpriteBatch m_pSpriteBatch;
        private CEnemyManager m_EnemyManager;

        //draw font stuff
        private SpriteFont m_Gameplayfont;
       
        public void Enter(ContentManager content, SpriteBatch spriteBatch)
        {
            //get a copy of the content manager and spritebatch
            m_pContent = content;
            m_pSpriteBatch = spriteBatch;

            //figure out the time for shadow stuff
            string s = DateTime.Now.ToString("H:mm:ss");

            //add and create Player 1
            CPlayer1 Player1;

            Game1.Instance.ObjectFactory.RegisterClassType<CPlayer1>("Player1");
            Player1 = (CPlayer1)Game1.Instance.ObjectFactory.CreateObject("Player1");
            Player1.Position = new Vector2(100, 100);
            Player1.Character = Game1.Instance.CharacterFactory.CreateObject(Game1.Instance.CharacterSelect[(int)CGlobal.Players.PLAYER1].ToString());
            Game1.Instance.ObjectManager.AddObject(Player1);

            //add and create Player 2
            CPlayer2 Player2;
            Game1.Instance.ObjectFactory.RegisterClassType<CPlayer2>("Player2");
            Player2 = (CPlayer2)Game1.Instance.ObjectFactory.CreateObject("Player2");
            Player2.Position = new Vector2(50, 100);
            Player2.Character = new CNinja();
            Game1.Instance.ObjectManager.AddObject(Player2);

            //tell the hud to update the health
            CMessageSystem.Instance.SendMessage(new CPlayer1HealthChange(Player1.Character.Health, Player1.Character.MaxHealth));
            CMessageSystem.Instance.SendMessage(new CPlayer2HealthChange(Player2.Character.Health, Player2.Character.MaxHealth));

            //add bullets
            Game1.Instance.ObjectFactory.RegisterClassType<CBulletBase>(CBase.type.BASEBULLET.ToString());
            Game1.Instance.ObjectFactory.RegisterClassType<CBulletBase>(CBase.type.ENEMYBULLET.ToString());

            //load the font
            m_Gameplayfont = content.Load<SpriteFont>("Fonts/ARCADECLASSIC");
   
            //add enemies
            Game1.Instance.ObjectFactory.RegisterClassType<CEnemy>("Enemy");
            m_EnemyManager = CEnemyManager.Instance;

            for (int i = 0; i < 10; ++i)
            {
                CEnemy enemy = (CEnemy)Game1.Instance.ObjectFactory.CreateObject("Enemy");

                if (i == 0)
                {

                    enemy.Velocity = new Vector2((float)CGlobal.Rand.NextDouble() - .5f, (float)CGlobal.Rand.NextDouble() - .5f);                
                    enemy.Leader = true;
                }
                else
                    enemy.Leader = false;

                enemy.Position = new Vector2(500 + (i * 10), 300);
                m_EnemyManager.ListOfEnemies.Add(enemy);
                Game1.Instance.ObjectManager.AddObject(enemy);
            }
                    }
        public void Input()
        {

            //get the input for all the objects
            Game1.Instance.ObjectManager.UpdateInput();
        }
        public void Update(TimeSpan fElapsedTime)
        {
            // update all objects
            Game1.Instance.ObjectManager.UpdateObjects((float)fElapsedTime.TotalSeconds);
            
            //update enemies
            m_EnemyManager.Update((float)fElapsedTime.TotalSeconds);
            
            //check for collision
            Game1.Instance.ObjectManager.CheckCollision();
        }
        public void Render()
        {
            
            //render all objects
            Game1.Instance.ObjectManager.RenderObjects();
        }
        public void Exit()
        {
            //clean up memory
            Game1.Instance.ObjectManager.RemoveAllObjects();
        }
    }
}
